package org.hanbo.code.repository.action;

import java.util.List;

import org.hanbo.code.repository.persistence.entity.CodeSnippet;

public class CodeSnippetActionBase extends GaeActionSupportBase
{
	private static final long serialVersionUID = 572636676590279965L;

	protected String subjectField;
	protected boolean viewByPublic;
	protected String languageField;
	protected String referenceUrlField;
	protected String keywordsField;
	protected String relatedMethodsField;
	protected String codeSnippetField;
	protected String relatedCommentsField;
	
	protected long snippetId;
	
	public void setSubjectField(String value)
	{
		this.subjectField = value;
	}
	
	public String getSubjectField()
	{
		return this.subjectField;
	}
	
	public void setViewByPublic(boolean viewByPublic)
	{
		this.viewByPublic = viewByPublic;
	}

	public boolean getViewByPublic()
	{
		return viewByPublic;
	}
		
	public void setLanguageField(String languageValue)
	{
		this.languageField = languageValue;
	}

	public String getLanguageField()
	{
		return this.languageField;
	}

	public void setReferenceUrlField(String value)
	{
		referenceUrlField = value;
	}
	
	public String getReferenceUrlField()
	{
		return referenceUrlField;
	}
	
	public void setKeywordsField(String value)
	{
		keywordsField = value;
	}
	
	public String getKeywordsField()
	{
		return keywordsField;
	}
	
	public void setRelatedMethodsField(String value)
	{
		relatedMethodsField = value;
	}

	public String getRelatedMethodsField()
	{
		return relatedMethodsField;
	}
	
	public void setCodeSnippetField(String value)
	{
		this.codeSnippetField = value;
	}
	
	public String getCodeSnippetField()
	{
		return this.codeSnippetField;
	}
	
	public void setRelatedCommentsField(String value)
	{
		this.relatedCommentsField = value;
	}

	public String getRelatedCommentsField()
	{
		return this.relatedCommentsField;
	}
	
	public void setSnippetId(long id)
	{
		this.snippetId = id;
	}
	
	public long getSnippetId()
	{
		return this.snippetId;
	}
	
	public CodeSnippet convertToCodeSnippet(String userEmail)
	{
		CodeSnippet snippetToSave = new CodeSnippet();
		snippetToSave.setAuthorEmail(userEmail);
		snippetToSave.setLanguageType(this.languageField);
		snippetToSave.createDateSubmitted();
		snippetToSave.setAvailabilityToPublic(this.viewByPublic);
		snippetToSave.setCodeComments(this.relatedCommentsField);
		snippetToSave.setCodeSnippet(this.codeSnippetField);
		List<String> keywordsList
			= CodeSnippet.convertStringToArrayList(this.keywordsField);
		snippetToSave.setKeywords(keywordsList);
		snippetToSave.setReferenceLink(this.referenceUrlField);
		List<String> relatedMethodsList
			= CodeSnippet.convertStringToArrayList(this.relatedMethodsField);
		snippetToSave.setRelatedMethods(relatedMethodsList);
		snippetToSave.setSubject(this.subjectField);
		
		return snippetToSave;
	}

	public void convertCodeSnippetToThis(CodeSnippet snippet)
	{
		this.codeSnippetField = snippet.getCodeSnippet();
		this.keywordsField = CodeSnippet.convertArrayListToString(snippet.getKeywords());
		this.languageField = snippet.getLanguageType();
		this.referenceUrlField = snippet.getReferenceLink();
		this.relatedCommentsField = snippet.getCodeComments();
		this.relatedMethodsField = CodeSnippet.convertArrayListToString(snippet.getRelatedMethods());
		this.subjectField = snippet.getSubject();
		this.snippetId = snippet.getId().getId();
		this.viewByPublic = snippet.isAvailableTpPublic();
	}
}
